package com.zuipin.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zuipin.entity.product.CatgroyPropVal;
import com.zuipin.entity.product.CatgroyPropValView;
import com.zuipin.entity.product.CatgroyPropView;
import com.zuipin.entity.product.CatgroyProperties;
import com.zuipin.entity.product.ProductInfo;
import com.zuipin.entity.product.PropertiesInfo;
import com.zuipin.entity.product.PropertiesVal;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.CategroyPropValMapper;
import com.zuipin.mapper.CategroyPropertiesMapper;
import com.zuipin.mapper.CatgroyPropValViewMapper;
import com.zuipin.mapper.CatgroyPropViewMapper;
import com.zuipin.mapper.PropertiesInfoMapper;
import com.zuipin.service.IProductInfoService;
import com.zuipin.service.IProductPropertyValueService;
import com.zuipin.service.IPropertiesInfoService;
import com.zuipin.service.IPropertiesValService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.util.StringUtil;
import com.zuipin.vo.BackPropValViewVo;
import com.zuipin.vo.BackPropViewVo;
import com.zuipin.vo.ModityBaseProp;
import com.zuipin.vo.ModityPropertiesValVo;
import com.zuipin.vo.PropDetailVo;
import com.zuipin.vo.PropInfoSearchVo;
import com.zuipin.vo.PropValVo;
import com.zuipin.vo.PropViewSearchVo;
import com.zuipin.vo.PropertiesInfoVo;

import net.sf.json.JSONObject;

/**
 * 属性信息业务类实现
 *
 * @ClassName PropertiesInfoServiceImpl
 * @author wangcongyi
 * @date 2017年4月19日 下午3:42:20
 */
@Service
public class PropertiesInfoServiceImpl implements IPropertiesInfoService {
	
	@Resource
	private PropertiesInfoMapper			propertiesInfoMapper;
	
	@Resource
	private IPropertiesValService			propertiesValService;
	
	@Resource
	private CategroyPropertiesMapper		categroyPropertiesMapper;
	
	@Resource
	private CategroyPropValMapper			categroyPropValMapper;
	
	@Resource
	private CatgroyPropViewMapper			catgroyPropViewMapper;
	
	@Resource
	private CatgroyPropValViewMapper		catgroyPropValViewMapper;
	
	@Resource
	private IProductPropertyValueService	productPropertyValueService;
	
	@Resource
	private IProductInfoService				productInfoService;
	
	@Override
	public int deleteByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.deleteByPrimaryKey(id);
	}
	
	@Override
	public int insert(PropertiesInfo record) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.insert(record);
	}
	
	@Override
	public int insertSelective(PropertiesInfo record) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.insertSelective(record);
	}
	
	@Override
	public PropertiesInfo selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public int updateByPrimaryKeySelective(PropertiesInfo record) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.updateByPrimaryKeySelective(record);
	}
	
	@Override
	public int updateByPrimaryKey(PropertiesInfo record) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.updateByPrimaryKey(record);
	}
	
	@Override
	public List<PropertiesInfoVo> selectPropInfo(JSONObject object) {
		// TODO Auto-generated method stub
		List<PropertiesInfoVo> vos = new ArrayList<PropertiesInfoVo>();
		List<PropertiesInfo> infos = propertiesInfoMapper.selectByCatId(object);
		ListIterator<PropertiesInfo> iterator = infos.listIterator();
		long isDisplay = object.getLong("isDisplay");
		while (iterator.hasNext()) {
			PropertiesInfoVo vo = new PropertiesInfoVo();
			PropertiesInfo info = iterator.next();
			vo.setId(info.getId());
			vo.setPropName(info.getPropName());
			object.put("propId", info.getId());
			List<PropertiesVal> vals = selectPropVal(object);
			// 判断该属性下是否存在属性值 或者 isDisplay为1的情况下都显示
			if (vals.size() > 0 || isDisplay != 0) {
				vo.setPropValList(vals);
				vos.add(vo);
			}
		}
		return vos;
	}
	
	@Override
	public List<PropertiesVal> selectPropVal(JSONObject object) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.selectPropVal(object);
	}
	
	@Override
	public List<PropValVo> selectPropValInfo(JSONObject object) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.selectPropValInfo(object);
	}
	
	@Override
	public PropertiesInfo isExists(PropertiesInfo prop) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.isExists(prop);
	}
	
	@Override
	public boolean modifyState(PropertiesInfo prop) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.modifyState(prop);
	}
	
	@Override
	public List<PropertiesInfo> selectByCatId(JSONObject object) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.selectByCatId(object);
	}
	
	/**
	 * searchVo里存在属性的一些基本信息条件进行查询
	 * 如果要查询分类属性信息则加个catId条件
	 * 如果要查询平台的分类属性信息则再加个sysId条件
	 * 
	 * 后台查询分类属性信息
	 * @author zhuangjiajian
	 * @date 2017-8-15 14:26:35
	 */
	@Override
	public Result<List<PropertiesInfo>> findPropInfo(PropInfoSearchVo propSearchVo, Pagination page) {
		
		PageResult<List<PropertiesInfo>> result = new PageResult<List<PropertiesInfo>>();
		
		List<PropertiesInfo> list = propertiesInfoMapper.findPropInfo(propSearchVo, page);
		
		if (list == null || list.isEmpty()) {
			
			result.setData(list);
			result.setTotal_record(0);
			result.setErrorCode(ErrorCode.SUCCESS);
			return result;
		}
		// 判断当前 多选项的属性是否有设置相应的属性值
		for (PropertiesInfo propertiesInfo : list) {
			
			if (propertiesInfo.getPropType() == PropertiesInfo.CHOOSE) {
				
				List<PropertiesVal> propVals = findPropVal(propertiesInfo.getId(), propSearchVo.getSysId(), propSearchVo.getCatId());
				if (propVals != null && !propVals.isEmpty()) {
					// 设置标记 已经有设置属性值
					propertiesInfo.setIsSetVal(1);
				} else {
					propertiesInfo.setIsSetVal(0);
				}
			}
			
			// 如果是分类下的属性 则返回的创建时间为分类属性添加的时间
			if (null != propSearchVo.getCatId()) {
				CatgroyProperties timeSearch = new CatgroyProperties();
				timeSearch.setCatId(propSearchVo.getCatId());
				timeSearch.setPropId(propertiesInfo.getId());
				timeSearch.setDelFlag(false);
				List<CatgroyProperties> cpTimes = categroyPropertiesMapper.selectBySelective(timeSearch);
				if (cpTimes != null && !cpTimes.isEmpty()) {
					propertiesInfo.setCreateTime(cpTimes.get(0).getCreateTime());
					propertiesInfo.setUpdateTime(cpTimes.get(0).getCreateTime());
				}
			}
		}
		
		result.setData(list);
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	/**
	 *  通过条件查询出分类属性值信息
	 *  @author zhuangjiajian
	 *  @date 2017-8-15 14:26:52
	 */
	@Override
	public List<PropertiesVal> findPropVal(Long propId, Long sysId, Long catId) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.findPropVal(propId, sysId, catId);
	}
	
	/**
	 * 查询属性的详情
	 * flag  是否查询基础属性对应的属性值
	 * @author zhuangjiajian
	 * @date 2017-8-15 14:27:04
	 * 
	 */
	@Override
	public PropDetailVo findPropInfoDetail(Long propId, Long catId, Long sysId) {
		
		PropDetailVo vo = new PropDetailVo();
		// 查询出对应的属性信息
		PropertiesInfo prop = propertiesInfoMapper.selectByPrimaryKey(propId);
		
		if (prop == null) {
			return null;
		}
		
		List<PropertiesVal> propVals = null;
		
		// 判断属性类型是否为多选项 如是则显示出相应的属性值
		if (prop.getPropType().equals(PropertiesInfo.CHOOSE)) {
			// 如果sysId参数为0 则查询 分类属性下的详情
			if (sysId == null || sysId == 0) {
				sysId = null;
			}
			propVals = propertiesInfoMapper.findPropVal(propId, sysId, catId);
		}
		
		// 填充信息到 VO上
		vo.setPropId(prop.getId());
		vo.setPropName(prop.getPropName());
		vo.setPropNo(prop.getPropNo());
		vo.setPropType(prop.getPropType().shortValue());
		vo.setRemark(prop.getRemark());
		vo.setPropVals(propVals);
		vo.setState(prop.getState());
		
		return vo;
	}
	
	/**
	 * 查询属性的详情
	 * @author zhuangjiajian
	 * @date 2017-8-15 14:27:04
	 * 
	 *//*
		@Override
		public Result<PropDetailVo> findPropInfoDetail(Long propId, Long catId, Long sysId) {
		Result<PropDetailVo> result = new Result<>();
		PropDetailVo vo = new PropDetailVo();
		// 查询出对应的属性信息
		PropertiesInfo prop = propertiesInfoMapper.selectByPrimaryKey(propId);
		
		if (prop == null) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setData(null);
			return result;
		}
		
		List<PropertiesVal> propVals = null;
		
		// 判断属性类型是否为多选项 如是则显示出相应的属性值
		if (prop.getPropType().equals(PropertiesInfo.CHOOSE)) {
			propVals = propertiesInfoMapper.findPropVal(propId, sysId, catId);
		}
		
		// 填充信息到 VO上
		vo.setPropId(prop.getId());
		vo.setPropName(prop.getPropName());
		vo.setPropNo(prop.getPropNo());
		vo.setPropType(prop.getPropType().shortValue());
		vo.setRemark(prop.getRemark());
		vo.setPropVals(propVals);
		vo.setState(prop.getState());
		
		result.setData(vo);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
		}*/
	
	/**
	 * 查询属性是否有被分类属性引用
	 * @author zhuangjiajian
	 * @date 2017-8-15 14:27:39
	 */
	@Override
	public Result<Boolean> isCanEditProp(Long propId) {
		
		Result<Boolean> result = new Result<Boolean>();
		CatgroyProperties search = new CatgroyProperties();
		
		search.setDelFlag(false);
		search.setPropId(propId);
		List<CatgroyProperties> list = categroyPropertiesMapper.selectBySelective(search);
		
		if (list == null || list.isEmpty()) {
			result.setData(true);
			result.setMessage("可以被编辑");
			result.setErrorCode(ErrorCode.SUCCESS);
		} else {
			result.setData(false);
			result.setMessage("不可被编辑");
			result.setErrorCode(ErrorCode.SUCCESS);
		}
		
		return result;
	}
	
	/**
	 * 更新属性详情信息
	 * @author zhuangjiajian
	 * @throws CustomException 
	 * @date 2017-8-15 14:28:25
	 */
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result<Boolean> updatePropAndVal(ModityBaseProp modityBaseProp) throws CustomException {
		Result<Boolean> result = new Result<Boolean>();
		if (modityBaseProp == null) {
			result.setErrorCode(ErrorCode.ILLEGAL_PARAM);
			result.setMessage("参数错误");
			result.setData(false);
			return result;
		}
		
		PropertiesInfo propInfo = propertiesInfoMapper.selectByPrimaryKey(modityBaseProp.getPropId());
		if (propInfo == null || propInfo.getDelFlag()) {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("查无属性");
			result.setData(false);
			return result;
		}
		
		if (PropertiesInfo.DISABLE.equals(propInfo.getState())) {
			
			result.setErrorCode(ErrorCode.PROP_IS_DISABLE);
			result.setMessage("属性被禁用不能修改");
			result.setData(false);
			return result;
			
		}
		
		// 更新属性信息
		propInfo.setUpdateId(modityBaseProp.getUpdateId());
		propInfo.setUpdateTime(DateUtils.getCurrentDateTime());
		propInfo.setPropName(modityBaseProp.getPropName());
		propInfo.setRemark(modityBaseProp.getRemark());
		// 修改类型
		Result<Boolean> res = isCanEditProp(modityBaseProp.getPropId());// 被引用 不能修改类型
		if (!propInfo.getPropType().equals(modityBaseProp.getPropType())) {
			if (res != null && !res.getData()) {
				result.setErrorCode(ErrorCode.MODITY_PROP_FAIL);
				result.setMessage("修改属性失败,被分类属性引用");
				result.setData(false);
				return result;
			}
		}
		
		propInfo.setPropType(modityBaseProp.getPropType());
		propertiesInfoMapper.updateByPrimaryKey(propInfo);
		List<PropertiesVal> valList = propertiesInfoMapper.findPropVal(modityBaseProp.getPropId(), null, null);
		
		// 如果是多选类型则更新属性值信息
		if (PropertiesInfo.CHOOSE.equals(modityBaseProp.getPropType())) {
			
			if (modityBaseProp.getPropValList() == null || modityBaseProp.getPropValList().size() < 1) {
				result.setErrorCode(ErrorCode.ILLEGAL_PARAM);
				result.setMessage("多选项,属性值不能为空");
				result.setData(false);
				return result;
			}
			
			Map<Long, String> map = new HashMap<>();
			
			// 如果被引用 判断属性值有没有修改过
			if (res != null && !res.getData() && valList != null) {
				for (ModityPropertiesValVo val : modityBaseProp.getPropValList()) {
					map.put(val.getId(), val.getPropVal());
				}
				Set<Long> keys = map.keySet();
				if (keys.size() != valList.size()) {
					// 修改过 新增或删除
					throw new CustomException(ErrorCode.MODITY_CAT_PROP_FAIL.getCode(), "被引用过不能修改属性值");
				}
				
				for (PropertiesVal propVal : valList) {
					String strVal = map.get(propVal.getId());
					map.remove(propVal.getId());
					if (StringUtil.isBlank(strVal) || !strVal.equals(propVal.getPropVal())) {
						throw new CustomException(ErrorCode.MODITY_CAT_PROP_FAIL.getCode(), "被引用过不能修改属性值");
					}
				}
				
			}
			
			/*
			// 删除原有的
			if (valList != null && !valList.isEmpty()) {
				for (PropertiesVal propVal : valList) {
					propVal.setDelFlag(true);
					propertiesValService.updateByPrimaryKeySelective(propVal);
				}
			}
			
			// 插入新的属性值信息
			List<PropertiesVal> newList = new ArrayList<>();
			for (ModityPropertiesValVo propVal : modityBaseProp.getPropValList()) {
				PropertiesVal val = new PropertiesVal();
				val.setCatPropId(propInfo.getId());
				val.setPropVal(propVal.getPropVal());
				val.setCreateId(modityBaseProp.getUpdateId());
				val.setUpdateId(modityBaseProp.getUpdateId());
				val.setUpdateTime(DateUtils.getCurrentDateTime());
				val.setCreateTime(DateUtils.getCurrentDateTime());
				val.setDelFlag(false);
				newList.add(val);
			}
			// 批量添加
			if (!newList.isEmpty()) {
				propertiesValService.insertBatch(newList);
			}*/
			
			// 更新属性值信息 ABC 变为AEF 则删除BC 增加EF信息
			List<String> oldVals = new ArrayList<String>();
			List<String> newVals = new ArrayList<String>();
			for (PropertiesVal v : valList) {
				oldVals.add(v.getPropVal());
			}
			for (ModityPropertiesValVo v : modityBaseProp.getPropValList()) {
				newVals.add(v.getPropVal());
			}
			
			// 删除BC
			if (valList != null && !valList.isEmpty()) {
				for (PropertiesVal propVal : valList) {
					// 如果新的属性值已经包含了 就不要执行删除动作
					if (newVals.contains(propVal.getPropVal())) {
						continue;
					}
					propVal.setDelFlag(true);
					propertiesValService.updateByPrimaryKeySelective(propVal);
				}
			}
			
			// 插入新的属性值信息 EF
			List<PropertiesVal> newList = new ArrayList<>();
			for (ModityPropertiesValVo propVal : modityBaseProp.getPropValList()) {
				
				// 如果已经存在旧的属性值中则不执行增加动作
				if (oldVals.contains(propVal.getPropVal())) {
					continue;
				}
				PropertiesVal val = new PropertiesVal();
				val.setCatPropId(propInfo.getId());
				val.setPropVal(propVal.getPropVal());
				val.setCreateId(modityBaseProp.getUpdateId());
				val.setUpdateId(modityBaseProp.getUpdateId());
				val.setUpdateTime(DateUtils.getCurrentDateTime());
				val.setCreateTime(DateUtils.getCurrentDateTime());
				val.setDelFlag(false);
				newList.add(val);
			}
			// 批量添加
			if (!newList.isEmpty()) {
				propertiesValService.insertBatch(newList);
			}
		}
		
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	/* 
	 *  添加或者更新属性详情信息
	 * @author zhuangjiajian
	 * @date 2017-8-15 14:28:25*/
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result<Boolean> addPropAndVal(String propVals, Long updateId, String propName, String propNo, Integer propType, String remark) throws CustomException {
		Result<Boolean> result = new Result<Boolean>();
		PropertiesInfo record = new PropertiesInfo();
		
		// 判断是否有属性编码
		if (propNo == null || propNo.equals("")) {
			// 自动生成
			propNo = "SX000" + (propertiesInfoMapper.getLastId() + 1);
			record.setPropNo(propNo);
		}
		
		// 判断是否有重复编号
		record.setPropNo(propNo);
		record.setDelFlag(false);
		List<PropertiesInfo> propInfos = selectBySelective(record);
		if (propInfos != null && !propInfos.isEmpty()) {
			result.setErrorCode(ErrorCode.OBJECT_EXIST);
			result.setMessage("此编号已经存在,请重新输入");
			result.setData(false);
			return result;
		}
		// 判断是否有重复属性名称
		PropertiesInfo isNameRepeat = new PropertiesInfo();
		isNameRepeat.setPropName(propName);
		isNameRepeat.setDelFlag(false);
		List<PropertiesInfo> isSameName = selectBySelective(isNameRepeat);
		if (isSameName != null && !isSameName.isEmpty()) {
			result.setErrorCode(ErrorCode.OBJECT_EXIST);
			result.setMessage("此属性名称已经存在,请重新输入");
			result.setData(false);
			return result;
		}
		
		//
		
		record.setPropCode("");
		record.setCreateId(updateId);
		record.setCreateTime(DateUtils.getCurrentDateTime());
		record.setUpdateId(updateId);
		record.setUpdateTime(DateUtils.getCurrentDateTime());
		record.setDelFlag(false);
		record.setPropName(propName);
		record.setPropType(propType);
		record.setRemark(remark);
		record.setState(PropertiesInfo.ENABLE);
		
		propertiesInfoMapper.insertSelective(record);
		
		// 如果是文本类型则添加属性值信息
		if (PropertiesInfo.CHOOSE.equals(propType)) {
			if (StringUtil.isBlank(propVals)) {
				throw new CustomException(ErrorCode.ILLEGAL_PARAM.getCode(), "多选项,属性值不能为空");
			}
			List<PropertiesVal> valList = new ArrayList<PropertiesVal>();
			for (String propVal : propVals.split(",")) {
				PropertiesVal val = new PropertiesVal();
				val.setCatPropId(record.getId());
				val.setPropVal(propVal);
				val.setCreateId(updateId);
				val.setCreateTime(DateUtils.getCurrentDateTime());
				val.setDelFlag(false);
				val.setUpdateId(updateId);
				val.setUpdateTime(DateUtils.getCurrentDateTime());
				valList.add(val);
			}
			// 批量添加
			propertiesValService.insertBatch(valList);
		}
		result.setData(true);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	/**
	 * @author zhuangjiajian
	 * @throws CustomException 
	 * @date 2017-8-15 14:29:35
	 * 更新分类属性（通用）
	 */
	@Override
	@Transactional(rollbackFor = { Exception.class })
	public Result updateCateProps(String propIds, Long catId, Long updateId) throws CustomException {
		
		Result result = new Result();
		List<Long> addProps = new ArrayList<Long>();
		if (!StringUtil.isBlank(propIds)) {
			for (String v : propIds.split(",")) {
				addProps.add(Long.parseLong(v));
			}
		}
		
		// 判断属性是否存在（是否在添加分类属性时 同时删除了基础属性信息）
		if (!StringUtil.isBlank(propIds)) {
			for (String propIdStr : propIds.split(",")) {
				
				PropertiesInfo propInfoExist = null;
				
				propInfoExist = propertiesInfoMapper.selectByPrimaryKey(Long.parseLong(propIdStr));
				
				if (propInfoExist == null || propInfoExist.getDelFlag()) {
					result.setErrorCode(ErrorCode.NOT_FIND);
					result.setMessage("查无属性ID:" + propIdStr);
					result.setData(false);
					return result;
				}
			}
		}
		
		// 删除旧的分类属性 信息
		CatgroyProperties del = new CatgroyProperties();
		del.setCatId(catId);
		del.setIsCommon(false);
		del.setDelFlag(false);
		List<CatgroyProperties> delList = categroyPropertiesMapper.selectBySelective(del);
		
		// 原本就有的属性集合
		List<Long> oldProps = new ArrayList<>();
		for (CatgroyProperties d : delList) {
			oldProps.add(d.getPropId());
			// 如果新增里也有包含此ID则不删除
			if (addProps.contains(d.getPropId())) {
				continue;
			}
			// 删除分类属性
			delCateProps(d.getPropId(), catId);
		}
		
		// 分类属性
		List<CatgroyProperties> addList = new ArrayList<CatgroyProperties>();
		
		// 要新增的Id
		/*List<Long> ids = new ArrayList<>();*/
		
		// 分类属性可见性
		// List<CatgroyPropView> viewAddList = new ArrayList<CatgroyPropView>();
		
		// 增加操作
		for (Long propId : addProps) {
			
			if (!oldProps.contains(propId)) {
				CatgroyProperties addVo = new CatgroyProperties();
				addVo.setCatId(catId);
				addVo.setPropId(propId);
				addVo.setIsCommon(false);
				addVo.setDelFlag(false);
				addVo.setUpdateTime(DateUtils.getCurrentDateTime());
				addVo.setUpdateId(updateId);
				addVo.setDisableSysIds("");
				addVo.setCreateTime(DateUtils.getCurrentDateTime());
				addVo.setCreateId(updateId);
				addList.add(addVo);
				// CatgroyPropView viewAddVo = new CatgroyPropView();
				// viewAddVo.setCatId(catId);
				// viewAddVo.setPropId(propId);
				// 默认不展示
				// viewAddVo.setViewType(0L);
				// viewAddVo.setDelFlag(false);
				// viewAddVo.setCreateId(updateId);
				// viewAddVo.setCreateTime(DateUtils.getCurrentDateTime());
				// viewAddVo.setUpdateId(updateId);
				// viewAddVo.setUpdateTime(DateUtils.getCurrentDateTime());
				// viewAddList.add(viewAddVo);
			}
		}
		// 批量添加
		if (!addList.isEmpty()) {
			/*List<PropertiesInfo> piList = propertiesInfoMapper.findByIds(ids);
			if (piList != null && piList.size() > 0) {
				String propNos = "";
				for (PropertiesInfo pi : piList) {
					propNos += pi.getPropNo();
				}
				throw new CustomException(ErrorCode.MODITY_CAT_PROP_FAIL.getCode(), "只有启用的属性才能引用[" + propNos + "]");
			}*/
			categroyPropertiesMapper.insertBatch(addList);
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("更新成功");
		result.setData(true);
		return result;
	}
	
	@Override
	public List<PropDetailVo> findPropAndVal(Long catId) {
		List<PropDetailVo> resultList = new ArrayList<PropDetailVo>();
		CatgroyProperties search = new CatgroyProperties();
		search.setCatId(catId);
		search.setIsCommon(false);
		search.setDelFlag(false);
		List<CatgroyProperties> list = categroyPropertiesMapper.selectBySelective(search);
		for (CatgroyProperties cp : list) {
			PropDetailVo vo = findPropInfoDetail(cp.getPropId(), catId, null);
			if (vo != null) {
				resultList.add(vo);
			}
		}
		return resultList;
	}
	
	@Override
	public List<PropertiesVal> findPropValByIdAndName(String search, Long propId) {
		// TODO Auto-generated method stub
		return propertiesValService.selectPropValByIDOrValName(search, propId);
	}
	
	@Override
	public List<PropertiesInfo> selectBySelective(PropertiesInfo search) {
		// TODO Auto-generated method stub
		return propertiesInfoMapper.selectBySelective(search);
	}
	
	@Transactional(rollbackFor = { Exception.class })
	@Override
	public Result<Boolean> updateCatePropVals(Long propId, String propValIds, Long catId, Long updateId) {
		Result<Boolean> result = new Result<Boolean>();
		CatgroyPropVal search = new CatgroyPropVal();
		search.setCatId(catId);
		search.setPropId(propId);
		
		search.setDelFlag(false);
		List<CatgroyPropVal> oldCatePropVals = categroyPropValMapper.selectBySelective(search);
		List<Long> oldPropValIds = new ArrayList<Long>();
		
		for (CatgroyPropVal v : oldCatePropVals) {
			v.setDelFlag(true);
			// 删除旧的分类属性值记录
			categroyPropValMapper.updateByPrimaryKeySelective(v);
			oldPropValIds.add(v.getPropValId());
		}
		
		// 删除包含被删除的分类属性值的的分类属性值可见性记录
		// A A1 A2 A3 平台sysId=2 A A1 A3 如果更新A的属性值为A1 A2 则删除对应的分类属性值可见性 为A3的记录
		if (!oldCatePropVals.isEmpty() && !StringUtil.isBlank(propValIds)) {
			oldPropValIds.removeAll(Arrays.asList(propValIds.split(",")));
			if (!oldPropValIds.isEmpty()) {
				List<Long> delPropValViews = oldPropValIds;
				// 删除这些属性值ID对应的分类属性值可见性记录
				List<CatgroyPropValView> delViews = new ArrayList<CatgroyPropValView>();
				for (Long delPropValId : delPropValViews) {
					CatgroyPropValView delObj = new CatgroyPropValView();
					delObj.setCatId(catId);
					delObj.setPropId(propId);
					delObj.setPropValId(delPropValId);
					delObj.setDelFlag(true);
					delViews.add(delObj);
				}
				catgroyPropValViewMapper.updateBatch(delViews);
			}
		}
		
		if (!StringUtil.isBlank(propValIds)) {
			// 增加新的属性值
			for (String str : propValIds.split(",")) {
				CatgroyPropVal addVo = new CatgroyPropVal();
				Long propValId = Long.parseLong(str);
				addVo.setPropId(propId);
				addVo.setCatId(catId);
				addVo.setPropValId(propValId);
				addVo.setCreateId(updateId);
				addVo.setCreateTime(DateUtils.getCurrentDateTime());
				addVo.setUpdateId(updateId);
				addVo.setUpdateTime(DateUtils.getCurrentDateTime());
				addVo.setDelFlag(false);
				addVo.setIsCommon(false);
				categroyPropValMapper.insertSelective(addVo);
			}
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(true);
		return result;
	}
	
	@Transactional(rollbackFor = { Exception.class })
	@Override
	public Result delCateProps(Long propId, Long catId) {
		Result result = new Result();
		
		// 删除分类属性
		categroyPropertiesMapper.delByCatIdAndPropId(propId, catId);
		PropertiesInfo isDisablePropObj = propertiesInfoMapper.selectByPrimaryKey(propId);
		
		// 删除 对应的商品管理-商品详情中该属性字段和属性值都消失）；
		productPropertyValueService.deleteByPropId(catId, propId);
		
		// 删除分类属性可见性
		List<CatgroyPropView> list = new ArrayList<CatgroyPropView>();
		CatgroyPropView cp = new CatgroyPropView();
		cp.setCatId(catId);
		cp.setPropId(propId);
		cp.setDelFlag(true);
		list.add(cp);
		catgroyPropViewMapper.updateBatch(list);
		
		// 删除分类属性值
		CatgroyPropVal delCateVal = new CatgroyPropVal();
		delCateVal.setCatId(catId);
		delCateVal.setPropId(propId);
		List<CatgroyPropVal> delCateValList = categroyPropValMapper.selectBySelective(delCateVal);
		for (CatgroyPropVal del : delCateValList) {
			del.setDelFlag(true);
			categroyPropValMapper.updateByPrimaryKeySelective(del);
		}
		
		// 删除分类属性值可见性
		CatgroyPropValView delCateValView = new CatgroyPropValView();
		delCateValView.setCatId(catId);
		delCateValView.setPropId(propId);
		List<CatgroyPropValView> delCateValViewList = catgroyPropValViewMapper.selectBySelective(delCateValView);
		for (CatgroyPropValView del : delCateValViewList) {
			del.setDelFlag(true);
		}
		if (delCateValViewList != null && !delCateValViewList.isEmpty()) {
			catgroyPropValViewMapper.updateBatch(delCateValViewList);
		}
		
		result.setData(true);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("删除成功");
		return result;
	}
	
	@Override
	public PageResult<List<BackPropViewVo>> showCatePropView(PropViewSearchVo searchVo, Pagination page) {
		
		PageResult<List<BackPropViewVo>> result = new PageResult<List<BackPropViewVo>>();
		
		List<BackPropViewVo> data = new ArrayList<BackPropViewVo>();
		// 查询分类属性（通用）下所有的属性信息
		List<PropertiesInfo> props = catgroyPropViewMapper.findPropView(page, searchVo);
		
		for (PropertiesInfo prop : props) {
			
			// 查出 PC详情 PC筛选 WAP详情 WAP筛选的可视状态
			BackPropViewVo vo = new BackPropViewVo();
			vo.setPropType(prop.getPropType());
			vo.setPropId(prop.getId());
			vo.setPropName(prop.getPropName());
			vo.setPropNo(prop.getPropNo());
			
			// PC详情
			CatgroyPropView pcDetailSearch = new CatgroyPropView();
			pcDetailSearch.setCatId(searchVo.getCatId());
			pcDetailSearch.setDelFlag(false);
			pcDetailSearch.setPropId(prop.getId());
			pcDetailSearch.setDevice(PropertiesInfo.PC);
			pcDetailSearch.setViewPosition(CatgroyPropView.DETAIL);
			pcDetailSearch.setViewType(CatgroyPropView.SHOW);
			List<CatgroyPropView> pcDetailList = catgroyPropViewMapper.selectBySelective(pcDetailSearch);
			
			if (pcDetailList != null && !pcDetailList.isEmpty()) {
				vo.setPcDetail(true);
			} else {
				vo.setPcDetail(false);
			}
			
			// PC筛选
			CatgroyPropView pcChooseSearch = new CatgroyPropView();
			pcChooseSearch.setCatId(searchVo.getCatId());
			pcChooseSearch.setDelFlag(false);
			pcChooseSearch.setPropId(prop.getId());
			pcChooseSearch.setDevice(PropertiesInfo.PC);
			pcChooseSearch.setViewPosition(CatgroyPropView.CHOOSE);
			pcChooseSearch.setViewType(CatgroyPropView.SHOW);
			List<CatgroyPropView> pcChooseList = catgroyPropViewMapper.selectBySelective(pcChooseSearch);
			if (pcChooseList != null && !pcChooseList.isEmpty()) {
				vo.setPcChoose(true);
			} else {
				vo.setPcChoose(false);
			}
			
			// WAP详情
			CatgroyPropView wapDetailSearch = new CatgroyPropView();
			wapDetailSearch.setCatId(searchVo.getCatId());
			wapDetailSearch.setDelFlag(false);
			wapDetailSearch.setPropId(prop.getId());
			wapDetailSearch.setDevice(PropertiesInfo.WAP);
			wapDetailSearch.setViewPosition(CatgroyPropView.DETAIL);
			wapDetailSearch.setViewType(CatgroyPropView.SHOW);
			List<CatgroyPropView> wapDetailList = catgroyPropViewMapper.selectBySelective(wapDetailSearch);
			if (wapDetailList != null && !wapDetailList.isEmpty()) {
				vo.setWapDetail(true);
			} else {
				vo.setWapDetail(false);
			}
			
			// WAP筛选
			CatgroyPropView wapChooseSearch = new CatgroyPropView();
			wapChooseSearch.setCatId(searchVo.getCatId());
			wapChooseSearch.setDelFlag(false);
			wapChooseSearch.setPropId(prop.getId());
			wapChooseSearch.setDevice(PropertiesInfo.WAP);
			wapChooseSearch.setViewPosition(CatgroyPropView.CHOOSE);
			wapChooseSearch.setViewType(CatgroyPropView.SHOW);
			List<CatgroyPropView> wapChooseList = catgroyPropViewMapper.selectBySelective(wapChooseSearch);
			if (wapChooseList != null && !wapChooseList.isEmpty()) {
				vo.setWapChoose(true);
			} else {
				vo.setWapChoose(false);
			}
			
			data.add(vo);
		}
		
		result.setData(data);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setTotal_record(page.getTotalRecord());
		return result;
	}
	
	@Transactional(rollbackFor = { Exception.class })
	@Override
	public Result<Boolean> setCatePropInfoViewAndPosition(Long updateId, Long catId, Long sysId, List<BackPropViewVo> propViewVo) {
		Result<Boolean> result = new Result<Boolean>();
		
		// 通过catId sysId propId 获取原有的可见性设置记录 删除旧的 增加新的
		CatgroyPropView search = new CatgroyPropView();
		search.setSysId(sysId);
		search.setCatId(catId);
		List<CatgroyPropView> oldList = catgroyPropViewMapper.selectBySelective(search);
		
		if (oldList != null && !oldList.isEmpty()) {
			for (CatgroyPropView del : oldList) {
				del.setDelFlag(true);
			}
			catgroyPropViewMapper.updateBatch(oldList);
		}
		
		for (BackPropViewVo backPropViewVo : propViewVo) {
			
			// 增加新的 可见性记录
			CatgroyPropView addView = new CatgroyPropView();
			addView.setCatId(catId);
			addView.setSysId(sysId);
			addView.setPropId(backPropViewVo.getPropId());
			addView.setDelFlag(false);
			addView.setCreateId(updateId);
			addView.setCreateTime(DateUtils.getCurrentDateTime());
			addView.setUpdateId(updateId);
			addView.setUpdateTime(DateUtils.getCurrentDateTime());
			
			// 如果下面都没有设置展示则默认为0 不展示
			addView.setViewType(CatgroyPropView.NOT_SHOW);
			
			if (backPropViewVo.getPcChoose()) {
				addView.setViewType(CatgroyPropView.SHOW);
				addView.setDevice(PropertiesInfo.PC);
				addView.setViewPosition(CatgroyPropView.CHOOSE);
				catgroyPropViewMapper.insert(addView);
			}
			if (backPropViewVo.getPcDetail()) {
				addView.setViewType(CatgroyPropView.SHOW);
				addView.setDevice(PropertiesInfo.PC);
				addView.setViewPosition(CatgroyPropView.DETAIL);
				catgroyPropViewMapper.insert(addView);
			}
			if (backPropViewVo.getWapChoose()) {
				addView.setViewType(CatgroyPropView.SHOW);
				addView.setDevice(PropertiesInfo.WAP);
				addView.setViewPosition(CatgroyPropView.CHOOSE);
				catgroyPropViewMapper.insert(addView);
			}
			if (backPropViewVo.getWapDetail()) {
				addView.setViewType(CatgroyPropView.SHOW);
				addView.setDevice(PropertiesInfo.WAP);
				addView.setViewPosition(CatgroyPropView.DETAIL);
				catgroyPropViewMapper.insert(addView);
			}
			
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setData(true);
		return result;
	}
	
	@Transactional(rollbackFor = { Exception.class })
	@Override
	public PageResult<List<BackPropValViewVo>> showCatePropValView(String device, Long sysId, Long catId, Long propId, Pagination page) {
		PageResult<List<BackPropValViewVo>> result = new PageResult<List<BackPropValViewVo>>();
		List<BackPropValViewVo> data = new ArrayList<BackPropValViewVo>();
		
		// 查询出通用的分类属性值 显示的列表
		List<CatgroyPropVal> valList = categroyPropValMapper.selectByCatIdAndPropId(catId, propId, page);
		
		for (CatgroyPropVal val : valList) {
			
			// 创建返回前台的vo
			BackPropValViewVo vo = new BackPropValViewVo();
			vo.setDevice(device);
			vo.setPropId(propId);
			vo.setPropValId(val.getPropValId());
			
			PropertiesVal propVal = propertiesValService.selectByPrimaryKey(val.getPropValId());
			if (propVal != null) {
				vo.setPropValName(propVal.getPropVal());
			}
			
			CatgroyPropValView search = new CatgroyPropValView();
			search.setCatId(catId);
			search.setPropId(propId);
			search.setPropValId(val.getPropValId());
			search.setViewPosition(CatgroyPropView.CHOOSE);
			search.setDevice(device);
			search.setDelFlag(false);
			search.setViewType(CatgroyPropValView.SHOW);
			List<CatgroyPropValView> valView = catgroyPropValViewMapper.selectBySelective(search);
			if (valView != null && !valView.isEmpty()) {
				vo.setIsDiplay(true);
			} else {
				vo.setIsDiplay(false);
			}
			
			data.add(vo);
		}
		
		/*	CatgroyPropValView search = new CatgroyPropValView();
			search.setCatId(catId);
			search.setPropId(propId);
			search.setDelFlag(false);
			// 查询出 分类属性值
			List<CatgroyPropValView> propVals = catgroyPropValViewMapper.selectByCatIdAndPropId(search, page);
			
			for (CatgroyPropValView propValView : propVals) {
				BackPropValViewVo vo = new BackPropValViewVo();
				vo.setPropValId(propValView.getPropValId());
				vo.setPropId(propId);
				PropertiesVal propVal = propertiesValService.selectByPrimaryKey(propValView.getPropValId());
				
				if (propVal != null) {
					vo.setPropValName(propVal.getPropVal());
				}
				
				if (propValView.getViewType().equals(CatgroyPropValView.NOT_SHOW)) {
					vo.setIsDiplay(false);
				} else {
					vo.setIsDiplay(true);
				}
				data.add(vo);
			}*/
		result.setData(data);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setTotal_record(page.getTotalRecord());
		return result;
		
	}
	
	@Override
	public Result setCatePropValView(String device, Long updateId, Long sysId, Long catId, Long propId, List<BackPropValViewVo> backPropValViewVo) {
		Result result = new Result();
		// 删除旧的 分类属性值可见性数据(区分设备)
		CatgroyPropValView record = new CatgroyPropValView();
		record.setCatId(catId);
		record.setPropId(propId);
		record.setDevice(device);
		record.setSysId(sysId);
		record.setDelFlag(false);
		
		List<CatgroyPropValView> oldList = catgroyPropValViewMapper.selectBySelective(record);
		if (oldList != null && !oldList.isEmpty()) {
			for (CatgroyPropValView del : oldList) {
				del.setDelFlag(true);
			}
			catgroyPropValViewMapper.updateBatch(oldList);
		}
		
		// 增加新的 分类属性值记录
		List<CatgroyPropValView> addList = new ArrayList<CatgroyPropValView>();
		if (backPropValViewVo != null && !backPropValViewVo.isEmpty()) {
			for (BackPropValViewVo addVo : backPropValViewVo) {
				CatgroyPropValView addRecord = new CatgroyPropValView();
				addRecord.setCatId(catId);
				addRecord.setPropId(propId);
				addRecord.setPropValId(addVo.getPropValId());
				addRecord.setSysId(sysId);
				
				addRecord.setDevice(device);
				addRecord.setViewPosition(CatgroyPropView.CHOOSE);
				
				addRecord.setCreateId(updateId);
				addRecord.setCreateTime(DateUtils.getCurrentDateTime());
				addRecord.setUpdateId(updateId);
				addRecord.setUpdateTime(DateUtils.getCurrentDateTime());
				if (addVo.getIsDiplay()) {
					addRecord.setViewType(CatgroyPropValView.SHOW);
				} else {
					addRecord.setViewType(CatgroyPropValView.NOT_SHOW);
				}
				addRecord.setDelFlag(false);
				addList.add(addRecord);
			}
		}
		if (!addList.isEmpty()) {
			catgroyPropValViewMapper.insertBatch(addList);
		}
		result.setData(true);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("分类属性值可见性更新成功");
		
		return result;
	}
	
	@Override
	public List<PropertiesVal> findPropValByPropId(Long propId) {
		// TODO Auto-generated method stub
		return propertiesValService.selectByPropId(propId);
	}
	
	/**
	 * 获取商品的  属性-属性值 列表
	 */
	@Override
	public List<PropValVo> getPropValList(Long sysId, String productSku, String device) {
		// 查找 平台属性已经展示的属性列表
		
		ProductInfo pro = productInfoService.selectByProSku(productSku);
		if (pro != null) {
			Long catId = pro.getCatId();
		}
		
		// 通过ProductProperties表 查询特定propId下面的值
		return null;
	}
	
}
